<template>
	<div class="address">
		<Header></Header>
		<van-address-edit
			:area-list="areaList"
			show-postal
			show-delete
			:address-info="addressInfo"
			show-set-default
			show-search-result
			:search-result="searchResult"
			:area-columns-placeholder="['请选择', '请选择', '请选择']"
			@save="onSave"
			@delete="onDelete"
		/>
	</div>
</template>

<script>
import Header from "@/components/header";
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
import { reqAddAddress } from "@/api/address";
export default {
	data() {
		return {
			areaList,
			searchResult: [],
			addressInfo: {
				name: "灰太狼",
				tel: "12213213213",
				province: "河南省",
				city: "郑州市",
				county: "惠济区",
				addressDetail: "金河校区",
				postalCode: "123422",
				isDefault: true,
			},
		};
	},
	computed: {},
	watch: {},

	methods: {
		async onSave(content) {
			// 获取用户对象信息
			console.log(content);
			const infoObj = {
				receiver: content.name,
				regions: content.province + content.city + content.county,
				address: content.addressDetail,
			};
			// 转为字符串
			const str = JSON.stringify(infoObj);
			// 设置到本地存储中.记得在App.vue中做持久化处理。
			localStorage.setItem("addressInfo", str);
			// 调用收货地址新增列表
			let params = {
				receiver: content.name,
				mobile: content.tel,
				regions: content.province + content.city + content.county,
				address: content.addressDetail,
				isDefault: content.isDefault,
			};
			// 调用接口
			const res = await reqAddAddress(params);
			console.log(res);
			if (res.code == "success") {
				Toast(res.message);
			}
			//跳转到购物车页面
			this.$router.push("/cart");
		},
		onDelete() {
			Toast("delete");
		},
	},
	created() {},
	mounted() {},
	components: {
		Header,
	},
};
</script>
<style scoped></style>
